package cn.szzsi._admin;

import cn.szzsi.common.kit.CommonUtil;
import cn.szzsi.common.model.Article;
import cn.szzsi.common.model.ArticleCategory;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;

import java.util.Date;
import java.util.List;

/**
 * 商户后台类 - 文章分类
 *
 */
public class ArticleCategoryController extends BaseAdminController<ArticleCategory> {
    private ArticleCategory articleCategory;
    private String shopId;

    // 列表
    public void list(){
        shopId=getAttr("shopId");
        List<ArticleCategory> articleCategoryTreeList = ArticleCategory.dao.getByUid(shopId);
        setAttr("articleCategoryTreeList",articleCategoryTreeList);
        render("article_category_list.html");
    }
    // 添加
    public void add() {
        shopId=getAttr("shopId");
        List<ArticleCategory> articleCategoryTreeList = ArticleCategory.dao.getByUid(shopId);
        setAttr("articleCategoryTreeList", articleCategoryTreeList);
        setAttr("shopId",shopId);
        render("article_category_input.html");
    }

    // 编辑
    public void edit() {
        shopId=getAttr("shopId");
        String id = getPara("id","");
        if(StrKit.notBlank(id)){
            setAttr("articleCategory", ArticleCategory.dao.findById(id));
        }
        setAttr("articleCategoryTreeList", ArticleCategory.dao.getByUid(shopId));
        setAttr("shopId",shopId);
        render("article_category_input.html");
    }

    // 添加
    public void save(){
        shopId=getAttr("shopId");
        articleCategory = getModel(ArticleCategory.class);
        articleCategory.set("uid",shopId);
        articleCategory.set("id", CommonUtil.getUUID());
        ArticleCategory parent = ArticleCategory.dao.findById(articleCategory.getStr("parent_id"));
        if (parent != null) {
            String parentPath = parent.getStr("path");
            articleCategory.set("path",parentPath + ArticleCategory.PATH_SEPARATOR + articleCategory.getStr("id"));
        } else {
            articleCategory.set("path",articleCategory.getStr("id"));
        }
        articleCategory.set("createDate", new Date());
        articleCategory.save();
        renderJson(Ret.ok("info","保存成功").set("url","list"));
    }

    // 修改
    public void update(){
        articleCategory = getModel(ArticleCategory.class);
        updated(articleCategory);
        renderJson(Ret.ok("info","修改成功").set("url","list"));
    }

    // 删除
    public void delete(){
        String id = getPara("id","");
        ArticleCategory articleCategory = ArticleCategory.dao.findById(id);
        List<ArticleCategory> childrenArticleCategoryList = articleCategory.getChildren();
        if (childrenArticleCategoryList != null && childrenArticleCategoryList.size() > 0) {
            ajaxJsonErrorMessage("此文章分类存在下级分类，删除失败!");
        }
        List<Article> articleList = articleCategory.getArticleList();
        if (articleList != null && articleList.size() > 0) {
            ajaxJsonErrorMessage("此文章分类下存在文章，删除失败!");
            return;
        }
        ArticleCategory.dao.deleteById(id);
        renderJson(Ret.ok());
        //ajaxJsonSuccessMessage("删除成功！");
    }

}
